import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ycbl/config/bl_color.dart';
import 'package:ycbl/config/bl_style.dart';
import 'package:ycbl/config/route_manager.dart';
import 'package:ycbl/utils/toast_util.dart';
import 'package:ycbl/utils/wx_util.dart';
import 'package:ycbl/widget/bl_app_bar.dart';
import 'package:ycbl/widget/border_label.dart';
import 'package:ycbl/widget/gradient_btn.dart';

import 'package:ycbl/widget/image.dart';

class LoginWayDemo extends StatefulWidget {
  //是否作为跟页面存在
  final bool isRootPage;

  const LoginWayDemo({Key key, @required this.isRootPage}) : super(key: key);

  @override
  _LoginWayDemoState createState() => _LoginWayDemoState();
}

class _LoginWayDemoState extends State<LoginWayDemo> {
  /// 微信登录后返回的code
  String _result;

  /// 跳转到验证码登录页面
  _go2CodeLogin(){
    Navigator.of(context).pushNamed(BLRouteName.login_code, arguments: false);
  }

  /// 先判断手机有没有安装微信
  _isInstall() async {
    var installed = await WxUtil().isWeChatInstalled();
    if (installed) {
      _wxAuth();
    } else {
      ToastUtil.showLongToast('没有安装微信APP');
    }
  }

  /// 执行登录操作
  _wxAuth() {
    WxUtil().sendAuth((code) {
      print("code:$code");
      setState(() {
        _result = code;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BLCommonWhiteAppBar(
        context: context,
        //如果当前是根页面，则隐藏返回按钮
        hideLeftIcon: widget.isRootPage,
        //如果当前是根页面，则显示直接进入按钮，否则不显示
        rightActions: widget.isRootPage ? [
          Padding(
            padding: EdgeInsets.fromLTRB(20, 14, 20, 14),
            child: InkWell(
              onTap: () {
                //进入主页
                Navigator.of(context)
                    .pushReplacementNamed(BLRouteName.mainNav);
                },
              child: BorderLabel(
                borderRadius: 20,
                text: '直接进入',
              ),
            ),
          ),
        ] : [],
        ),
        backgroundColor: BLColor.whiteColor,
        body: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              commonText('英才百利',
                TextStyle(fontSize: 24,fontWeight: FontWeight.bold,color: Colors.black),
                false
              ),
              commonText('初高中学科辅导及',
                TextStyle(fontSize: 20,fontWeight: FontWeight.bold,color: BLColor.commonTextColor),
                false
              ),
              commonText('高中升学计划',
                TextStyle(fontSize: 20,fontWeight: FontWeight.bold,color: BLColor.commonTextColor),
                true
              ),
              LocalImage('pic_login_bg.png'),
              Padding(
                padding: EdgeInsets.fromLTRB(50, 10, 50, 10),
                child: GradientBtn(
                    text: '微信登录',
                    style: BLStyle.middleWhiteTextStyle,
                    gradientDirection: GradientBtn.Top_Bottom,
                    padding: EdgeInsets.symmetric(vertical: 10,),
                    onPressed: () => _isInstall()
                ),
              ),
              Padding(
                padding: EdgeInsets.fromLTRB(50, 0, 50, 10),
                child: GradientBtn(
                  colors: [Color(0xFFEEEEEE), Color(0xFFEEEEEE),],
                  text: '手机号登录',
                  style: BLStyle.middleTextStyle,
                  gradientDirection: GradientBtn.Top_Bottom,
                  padding: EdgeInsets.symmetric(vertical: 10,),
                  onPressed: _go2CodeLogin,
                ),
              ),
              Text('微信登录拿到的code：$_result')
            ],
          ),
        )
    );
  }

  Widget commonText(String text,TextStyle mStyle,bool isBottom){
    return Padding(
      padding: EdgeInsets.fromLTRB(30, isBottom ? 0:10, 30, 10),
      child: Text(text, style: mStyle),
    );
  }
}
